package com.dsg.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.system.DsgSysParameter;
import com.dsg.service.system.IDsgSysParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 參數表 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/system/dsgSysParameter")
@Slf4j
@Api(description = "參數表")
public class DsgSysParameterController extends BaseController {

    @Autowired
    private IDsgSysParameterService service;

    @GetMapping(value = "/gen")
    @ApiOperation("生成參數")
    public AjaxResult gen() {
        List<DsgSysParameter> dsgSysParameterList = new ArrayList<>();
        String ss = "奧迪\n" +
                "AC Schnitzer\n" +
                "Artega\n" +
                "重汽王牌";
        List<String> list = Arrays.asList(ss.split("\n"));
        int i = 0;
        for (String s : list) {
            i++;
            DsgSysParameter p = new DsgSysParameter();
            p.setOid(UUIDGenerator.getUUID());
            p.setParaType("DSG_APPLY_CAR_CAR_BRAND");
            p.setParaName("車輛品牌");
            p.setParaKey(i+"");
            p.setParaValue(s);
            p.setStatus(1);
            dsgSysParameterList.add(p);
        }
        service.saveBatch(dsgSysParameterList);

        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgSysParameterList, dsgSysParameterList.size());
        return ajaxResult;
    }

    /**
     * 查詢緩存參數下拉
     *
     * @param paraType
     * @return
     */
    @GetMapping(value = "/getSysParameter")
    @ApiOperation("查詢緩存參數下拉")
    public AjaxResult getSysParameter(@ApiParam(value = "參數類別", name = "paraType", required = true) @RequestParam String paraType) {
        List<DsgSysParameter> dsgSysParameterList = service.selectByList(paraType);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgSysParameterList, dsgSysParameterList.size());
        return ajaxResult;
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgSysParameter
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgSysParameter dsgSysParameter) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("PARA_TYPE");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        //DSG_SYS_PARAMETER_STATUS
        page = service.selectDsgSysParameterList(page, dsgSysParameter);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增
     *
     * @param dsgSysParameter
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgSysParameter dsgSysParameter) {
        dsgSysParameter.setOid(UUIDGenerator.getUUID());
        //參數
        QueryWrapper<DsgSysParameter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARA_TYPE", dsgSysParameter.getParaType());
        queryWrapper.eq("PARA_KEY", dsgSysParameter.getParaKey());
        if (service.count(queryWrapper) > 0) {
            return setErrorJson("參數已經存在！");
        }
        boolean flag = service.save(dsgSysParameter);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 查閱
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查閱")
    public AjaxResult view(@PathVariable String id) {
        DsgSysParameter dsgSysParameter = service.getById(id);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgSysParameter);
    }

    /**
     * 更新
     *
     * @param dsgSysParameter
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgSysParameter dsgSysParameter) {
        AjaxResult ajaxResult;
        //參數
        QueryWrapper<DsgSysParameter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARA_TYPE", dsgSysParameter.getParaType());
        queryWrapper.eq("PARA_KEY", dsgSysParameter.getParaKey());
        queryWrapper.ne("OID", dsgSysParameter.getOid());
        if (service.count(queryWrapper) > 0) {
            return setErrorJson("參數已經存在！");
        }
        boolean flag = service.updateById(dsgSysParameter);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgSysParameter);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = service.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
